package com.session.filter;

import java.io.IOException;

import javax.inject.Inject;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.iman.Confucius.domain.SessionBean;
import com.iman.Confucius.domain.User;
import com.iman.Confucius.service.UserService;
//import com.iman.Confucius.util.Web7Util;

public class AuthFilter implements Filter {
	
	@Inject
	private UserService userService;
	private String encoding;
	private  Logger logger = LoggerFactory.getLogger(AuthFilter.class);

	public AuthFilter() {
	}

	public void init(FilterConfig filterconfig) throws ServletException {
		encoding = "utf-8";
	}

	public void doFilter(ServletRequest servletrequest, 	ServletResponse servletresponse, FilterChain filterchain) throws IOException, ServletException {
		
		HttpServletRequest req = (HttpServletRequest) servletrequest;
		HttpSession session = req.getSession();
		HttpServletResponse resp = (HttpServletResponse) servletresponse;
		SessionBean	 sessionBean = (SessionBean)session.getAttribute(SessionBean.USER_SESSION_KEY_PERFIX+SessionBean.USER_SESSION_SUBKEY_SESSION );
		if(sessionBean.getUser()!= null && ( sessionBean.getUser().getUserType() == 5 ||sessionBean.getUser().getUserType() == 3 )){
			RequestDispatcher dispatcher = req.getRequestDispatcher("/student/init");
			dispatcher.forward(req, resp); 
			return ;
		}
		
		filterchain.doFilter(servletrequest, servletresponse);
	}

	public void destroy() {

	}
}